<?php

namespace App\ApiBackend\Role;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 *  角色列表
 */
class Page extends ApiBaseController
{
  private array $searchMap = [
    "roleName" => ["aa.roleName", "like"],
    "status" => ["aa.status", "="],
  ];


  /**
   * 参数检查
   */
  protected function check(): void
  {

  }

  /**
   * 业务主体
   */
  protected function service(): void
  {

    //基础查询构造器
    $selectFields = [
      "aa.*",
    ];
    $queryConstructor = DB::table('admin_role as aa')
      ->select($selectFields)
      ->orderBy("aa.id", "ASC");
    //搜索条件
    $this->querySearchConstructor($queryConstructor, $this->searchMap);

    //是否需要分页
    $isPage = strpos(request()->path(), "page") ? 1 : 0;
    if ($isPage) {// 分页查询
      $total = $queryConstructor->count();
      $pageNumber = \request("pageNum", 1);
      $pageSize = \request("pageSize", 20);
      $queryConstructor->offset(($pageNumber - 1) * $pageSize)->limit($pageSize);
    }

    $listData = $queryConstructor->get();
    empty($listData) && $listData = [];

    //返回结果
    if ($isPage) {// 分页查询
      $this->result["data"]["list"] = $listData;
      $this->result["data"]["total"] = $total;
    } else {
      $this->result["data"] = $listData;
    }
    return;
  }
}
